import { Result } from '@/base/Result';
import { request } from '@/base/Request';
import { Page } from '@/base/Page';
import { Member, Member2, Data, Data3, PersionInformation, PersionRecord, PersionOrder ,PersionDetail} from '@/base/model/Member';
import { MemberSpecific, MemberSpecific2, MemberSpecific3 } from '@/base/model/MemberSpecific';
import { TableSort } from '@/base/TableSort';
import { MemberNews } from '@/base/model/MemberNews';
import { Label, LabelData, WeekData, ProvinceData, ModelData, DataCope } from '@/base/model/Label';
import { PointList } from '@/base/model/PointList';
// 查询会员数据
export function getMemberList(params: {
  pageIndex: number,
  pageSize: number,
  phone?: string,
  createBegin?: string,
  createEnd?: string,
  companyId?: number[]
}): Promise<Result<Member2[]>> {
  Object.assign(params, TableSort());
  let companyIdString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  return request.getArray(Member2, '/api/member/findPhoneAll?' + companyIdString, { params });
}
export function getNewsMemberList(params: {
  pageIndex: number,
  pageSize: number,
  phone?: string,
  // createBegin?: string,
  // createEnd?: string,
  companyId?: number[],
  newsId: string | null
}): Promise<Result<Page<MemberNews>>> {
  Object.assign(params, TableSort());
  let companyIdString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  return request.getPage(MemberNews, '/api/news/memberNews?' + companyIdString, { params });
}
export function getMember(id: number): Promise<Result<Member>> {
  return request.get(Member, `/api/member/${id}`);
}
export function getMemberInfo(id: number): Promise<Result<Member>> {
  return request.get(Member, `/api/member-info/selectById/${id}`);
}

export function putMember(id: number, params: {
  password: string,
  phone: string
}): Promise<Result<Member>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Member, `/api/member/${id}`, formData);
}

export function postMember(params: {
  password: string,
  phone: string
}): Promise<Result<Member>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Member, '/api/member', formData);
}
export function putMember2(params: {
  id: number,
  companyId: number,
  agentId: number,
  phone: string,
  nickname: string
  birthday: string
  sex: string,
  commentStatus: string,
  height: string,
  weight: string,
  label: string,
  career: string,
  hobby: string,
}): Promise<Result<Member>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(Member, `/api/member-info/specific`, formData);
}

export function postMember2(params: {
  companyId: number,
  agentId: number,
  phone: string,
  nickname: string
  birthday: string
  sex: string,
  career: string,
  hobby: string,
}): Promise<Result<Member>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Member, '/api/member/addMember ', formData);
}

export function deleteMember(id: number): Promise<Result<void>> {
  return request.delete(`/api/member/${id}`);
}

export function getMemberNum(): Promise<Result<Number>> {
  return request.get(Number, '/api/member/num/company');
}
export function getMemberSpecificList(params: {
  companyId?: Number,
  agentId?: Number,
  agentName?: string,
  pageIndex: number,
  pageSize: number,
  nickname?: string,
  phone?: string,
  sex?: string,
  modelName?: string,
  province?: string,
  city?: string,
  birthdayDate?: string,
  createBegin?: string,
  createEnd?: string,
  sortProperties?: string,
  sortDirection?: string,
}) {
  // console.log(params);
  return request.getPage(MemberSpecific, '/api/member/fetchMemberSpecific?', { params });
}
export function fetechPointList(params: {
  companyId: number,
  pageIndex: number,
  pageSize: number,
  phone: string,
  createBegin?: string,
  createEnd?: string,
  description: number
}) {
  return request.getPage(PointList, '/api/mall/fetchPointList', { params });
}
export function selectMemberByAgentId(params: {
  agentId: number,
}) {
  return request.getArray(MemberSpecific, '/api/member/selectMemberByAgentId?', { params });
}
// 会员性别统计
export function getPeopleSex(): Promise<Result<any>> {
  return request.get(Number, '/api/member/people/count');
}
// 会员兴趣统计
export function getPeopleHobby(): Promise<Result<Array<LabelData>>> {
  return request.getArray(LabelData, '/api/member/hobby/count');
}
// 会员职业统计
export function getPeopleCareer(): Promise<Result<Array<LabelData>>> {
  return request.getArray(LabelData, '/api/member/career/count');
}
// 会员注册时间
export function getPeopleRegister(params: {
  startTime: any,
  endTime: any
}) {
  return request.getArray(Data, '/api/member/register/count', { params });
}
// 周增长统计
export function getMemberWeekRegister(params: {
  startTime: any,
  endTime: any,
  region: string,
  province: string,
  modelId: number
}) {
  return request.getArray(WeekData, '/api/member/weekRegister/count', { params });
}
export function queryBalance() {
  return request.get(Number, '/api/mall/agentPoint');
}
export function savePoint(params: {
  num: number,
  phone: string
}) {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Number, '/api/mall/rewardPoint', formData);
}
// 获取省份
export function getProvince() {
  return request.getArray(ProvinceData, '/api/area/province');
}
// 获取型号
export function getModel() {
  return request.getArray(ModelData, '/api/device-model/model-all');
}
// 查询积分明细
export const integral = (params: {
  pageIndex: number,
  pageSize: number,
  phone: string
}) => request.get(String, `/api/member-info/pointList?pageIndex=${params.pageIndex}&pageSize=${params.pageSize}&phone=${params.phone}`)
// 设备增长
export function getDeviceRegion(params: {
  createBegin: any,
  createEnd: any,
  province: any,
  range: number
}) {
  return request.getArray(Data3, '/api/device/region/count', { params });
}
// 会员增长
export function getMemberRegion(params: {
  createBegin: any,
  createEnd: any,
  province: any,
  range: number
}) {
  return request.getArray(Data3, '/api/member/region/count', { params });
}
// 关联设备：/member/member-device-information/{memberId}
export function deviceInformation(memberId: number) {
  return request.getArray(PersionInformation, `/api/member/member-device-information/${memberId}`)
}

// 使用记录：/membermember-massage-record/{memberId}

export function massageRecord(memberId: number) {
  return request.getArray(PersionRecord, `/api/member/member-massage-record/${memberId}`)
}
// 订单记录：/post-sale/fetchMaintainList

export function fetchMaintainList(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection: string,
  orderState: number,
  phone:number,
  createBegin:string,
  createEnd:string
}) {
  return request.getArray(PersionOrder, `/api/post-sale/fetchOrderList?pageIndex=${params.pageIndex}&pageSize=${params.pageSize}&sortDirection=${params.sortDirection}&orderState=${params.orderState}&phone=${params.phone}&createBegin=${params.createBegin}&createEnd=${params.createEnd}`)
}

// 订单详细：/post-sale/fetchOrderDetail
export function fetchOrderDetail(orderId:number){
   return request.getArray(PersionDetail,`/api/post-sale/fetchOrderDetail?orderId=${orderId}`)
}

// 个人总积分排名
export function getMemberPointRanking(params: {
  pageIndex: number,
  pageSize: number,
  // phone?: string,
  // nickname?: string,
  // createBegin?: string,
  // createEnd?: string,
  // companyId?: number[]
}): Promise<Result<Page<MemberSpecific2>>> {
  // Object.assign(params, TableSort());
  let companyIdString = '';
  // if (params.companyId && params.companyId.length > 0) {
  //   companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
  //   delete params.companyId;
  // }
  return request.getPage(MemberSpecific2, '/api/mall/fetchPointRanking?' + companyIdString, { params });
}
// 个人签到积分排名
export function getMemberRanking(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection?: string,
  sortProperties?: string,
  // createBegin?: string,
  // createEnd?: string,
  // companyId?: number[]
}): Promise<Result<Page<MemberSpecific3>>> {
  // Object.assign(params, TableSort());
  let companyIdString = '';
  // if (params.companyId && params.companyId.length > 0) {
  //   companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
  //   delete params.companyId;
  // }
  return request.getPage(MemberSpecific3, '/api/sign-log/personalRanking?' + companyIdString, { params });
}
